<?php

App::uses('AppController', 'Controller');

class UsersController extends AppController {

    public $layout = 'correspondencia';
    public $uses = array('User','Area');

//    public function beforeFilter() {
//        parent::beforeFilter();
//        $this->Auth->allow();
//    }

    public function index() {
        $users = $this->User->find('all', array('recursive' => -1, 'limit' => 50, 'order' => array('User.id DESC')));
        $this->set(compact('users'));
    }

    public function insertar() {
        if (!empty($this->request->data)) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('Usuario guardado correctamente');
                $this->redirect(array('action' => 'index'), null, true);
            } else {
                $this->Session->setFlash('no se guardo', msgerror);
            }
        }
        $areas=$this->Area ->find('list',array('fields'=>'Area.nombre'));
        $this->set (compact('areas'));
    }

    public function editar($id = null) {
         $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalido'));
        }
        if (!empty($this->request->data)) {
            //debug($this->request->data);exit;
            $this->User->id = $id;
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('se guardo correctamente');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error. Intentelo de nuevo');
                $this->redirect(array('action' => 'index'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
         $areas=$this->Area ->find('list',array('fields'=>'Area.nombre'));
        $this->set (compact('areas'));
    }

    public function eliminar($id) {
        if ($this->request->data('post')) {
            throw new MethodNotAllowedException();
        }
        if ($this->User->delete($id)) {
            $this->Session->setFlash('Eliminado correctamente');
            $this->redirect(array('action' => 'index'), null, true);
        }
        
    }
    
    
    public function nuevo() {
        
    }

    public function listado() {
        
    }

    public function verifica() {
        
    }

    public function login() {
        
        $this->layout = 'login';
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                //return $this->redirect($this->Auth->redirect());
                $this->redirect(array('controller'=>'Users', 'action'=>'index'));
            }
            $this->Session->setFlash(__('Usuario o Password invalidos'));
        }
    }

    public function logout() {
        return $this->redirect($this->Auth->logout());
    }

}
